package com.king.juc.shared.safe;

import java.util.ArrayList;
import java.util.List;

/**
 * @author king
 * @date 2020/10/28 16:00
 */
public class ThreadSafe {

    /**
     * 加上final防止子类重写覆盖
     * @param loopNumber
     */
//    public void method1(int loopNumber){
    public final void method1(int loopNumber){
        List<String> list = new ArrayList<>();
        for (int i = 0; i < loopNumber; i++) {
            method2(list);
            method3(list);
        }
    }

    /**
     * 修饰符可以在一定程度上保护线程安全
     * @param list
     */
//    private void method3(List<String> list) {
    public void method3(List<String> list) {
        list.remove(0);
    }

//    private void method2(List<String> list) {
public void method2(List<String> list) {
        list.add("1");
    }
}
